package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.ReturnListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Sanmier
 * @create 2020-12-29 19:44
 */
@Service
public class ReturnListServiceImpl implements ReturnListService {

    @Autowired
    private ReturnListGoodsDao returnListGoodsDao;

    @Autowired
    private GoodsDao goodsDao;

    //新增退货单
    @Override
    public ServiceVO returnListGoodsListSave(ReturnList returnList, List<ReturnListGoods> returnListGoodsList) {

        //保存新增退货单
        returnListGoodsDao.saveReturnList(returnList);
        Integer returnListId = returnList.getReturnListId();

        //保存商品信息
        for (ReturnListGoods returnListGoods : returnListGoodsList) {
            //设置进货单
            returnListGoods.setReturnListId(returnListId);
            //调用保存方法
            returnListGoodsDao.saveReturnListGoods(returnListGoods);
            //查询状态
            Goods reGoods = goodsDao.findByGoodsId(returnListGoods.getGoodsId());

            //设置新总库存
            reGoods.setInventoryQuantity(reGoods.getInventoryQuantity() - returnListGoods.getGoodsNum());
            reGoods.setState(2);

            //调用修改方法
            goodsDao.updateGoods(reGoods);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


    //条件查询
    @Override
    public Map<String, Object> getReSearchList(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        //创建map集合
        Map<String, Object> map = new HashMap<>();
        //查询结果返回列表
        List<ReturnList> returnListList = returnListGoodsDao.getReSearchList(returnNumber, supplierId, state, sTime, eTime);
        //将查询结果放入map
        map.put("rows", returnListList);
        //返回map数据
        return map;
    }


    //查询退货单商品信息
    @Override
    public Map<String, Object> getReGoodsList(Integer returnListId) {
        //创建map集合
        Map<String, Object> map = new HashMap<>();

        //查询结果返回对象
        List<ReturnListGoods> returnListGoods = returnListGoodsDao.getReGoodsList(returnListId);
        map.put("rows",returnListGoods);
        return map;
    }


    //退货单删除
    @Override
    public ServiceVO deleteReturnListGoods(Integer returnListId) {
        returnListGoodsDao.deleteReturnListGoods(returnListId);
        returnListGoodsDao.deleteReturnList(returnListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


    //支付结算（修改退货单付款状态）
    @Override
    public ServiceVO updateReturneListGoods(Integer returnListId) {
        returnListGoodsDao.updateReturneList(returnListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
